
// 思路二：在循环体内排除没有目标值的区间
int searchInsert(int* nums, int numsSize, int target) {
    if (nums == NULL) return -1;
    if (numsSize == 0) return 0;
    // 二分查找第一个大于等于 target 的索引
    int left = 0;
    int right = numsSize;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (target > nums[mid]) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    return left;
}

// 思路一：在循环体内查找目标值
int searchInsert1(int* nums, int numsSize, int target) {
    if (nums == NULL) return -1;
    if (numsSize == 0) return 0;
    if (target > nums[numsSize - 1]) return numsSize;
    // 二分查找第一个大于等于 target 的索引
    int left = 0;
    int right = numsSize - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (target <= nums[mid]) {
            if (mid == 0 || nums[mid - 1] < target) return mid;
            else right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return -1;
}